package AVLTree;

import Map.Map;

public class BSTMap<K extends Comparable<K>, V> implements Map<K, V> {

    private class Node {
        private K key;
        private V val;
        private Node left, right;

        private Node(K key, V val) {
            this.key = key;
            this.val = val;
        }
    }

    private Node root;
    private int size;

    public BSTMap() {
        root = null;
        size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void add(K key, V val) {

        root = add(root, key, val);
    }

    private Node add(Node node, K key, V val) {
        if (node == null) {
            size++;
            return new Node(key, val);
        }

        if (node.key.compareTo(key) < 0) {
            node.right = add(node.right, key, val);
            return node;

        } else {
            node.left = add(node.left, key, val);
            return node;

        }
    }

    private K minmum() {
        if (size == 0) {
            throw new IllegalArgumentException("BSTMap is empty");
        }

        return minmum(root).key;
    }

    private Node minmum(Node node) {
        if (node.left == null) {
            return node;
        }

        return minmum(node.left);
    }

    private K maxmum() {
        if (size == 0) {
            throw new IllegalArgumentException("BSTMap is empty");
        }

        return maxmum(root).key;
    }

    private Node maxmum(Node node) {
        if (node.right == null) {
            return node;
        }

        return maxmum(node.right);
    }

    public K removeMin() {
        if (size == 0) {
            throw new IllegalArgumentException("BSTMap is empty");
        }
        K res = minmum();
        root = removeMin(root);
        return res;
    }

    private Node removeMin(Node node) {
        if (node.left == null) {
            size--;
            return node.right;
        }

        node.left = removeMin(node.left);
        return node;
    }

    public K removeMax() {
        if (size == 0) {
            throw new IllegalArgumentException("BSTMap is empty");
        }
        K res = maxmum();
        root = removeMax(root);
        return res;
    }

    private Node removeMax(Node node) {
        if (node.right == null) {
            size--;
            return node.left;
        }

        node.right = removeMin(node.right);
        return node;
    }


    @Override
    public void remove(K key) {
        if (size == 0) {
            throw new IllegalArgumentException("BSTMap is empty");
        }
        root = remove(root, key);
        size--;
    }

    private Node remove(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (key.compareTo(node.key) < 0) {
            node.left = remove(node.left, key);
            return node;

        } else if (key.compareTo(node.key) > 0) {
            node.right = remove(node.right, key);
            return node;

        } else {//key == node.key
            if (node.left == null) {
                return node.right;
            }

            if (node.right == null) {
                return node.left;
            }

            Node sucesstor = minmum(node.right);
            removeMin(node.right);
            node.key = sucesstor.key;
            node.val = sucesstor.val;
            return node;
        }
    }

    private Node getNode(Node node, K key) {
        if (node == null) {
            return null;
        }
        if (node.key == key) {
            return node;
        }

        if (node.key.compareTo(key) > 0) {
            return getNode(node.left, key);

        } else {
            return getNode(node.right, key);

        }
    }

    @Override
    public void set(K key, V val) {
        getNode(root, key).val = val;
    }

    @Override
    public V get(K key) {

        return getNode(root, key).val;
    }

    @Override
    public boolean contains(K key) {
        if (getNode(root, key) == null) {
            return false;
        } else {
            return true;
        }
    }
}
